package com.wave.traffic.modbus;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Client {
    public static void main(String[] args) {
        try {
            System.out.println("尝试连接");
            Socket s = new Socket("192.168.5.239", 502);
            System.out.println("连接成功！");
            //构建IO
            InputStream is = s.getInputStream();
            OutputStream os = s.getOutputStream();

//                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            BufferedOutputStream bw = new BufferedOutputStream( os );
            //向服务器端发送一条消息
            //String input = "000100000006FF0100640004";
            String input = "000100000006FF0100660001";
            byte[] byteInput = hexStr2bytes( input );
            System.out.println("hex string "+ bytesToHexString(byteInput));

            bw.write( byteInput );
            bw.flush();

            //读取服务器返回的消息
//                BufferedReader br = new BufferedReader(new InputStreamReader(is));
//                String mess = br.readLine();

            BufferedInputStream bis = new BufferedInputStream( is );
            byte[] bytes = new byte[10]; // 一次读取一个byte
            String ret = "";
            bis.read(bytes);
            ret += bytesToHexString(bytes) + " ";  //调用字节转化16进制字符串方法


            System.out.println("服务器："+ret);


            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar nowTime = Calendar.getInstance();
            nowTime.add(Calendar.SECOND, 10);
            System.out.println(sdf.format(nowTime.getTime()));
            System.out.println(nowTime.getTime().getTime());
            System.out.println(System.currentTimeMillis());

            while (System.currentTimeMillis() < nowTime.getTime().getTime()){
                System.out.println(System.currentTimeMillis());
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static byte[] hexStr2bytes(String hexStr) {

        if(hexStr.length()%2 != 0) {//长度为单数
            hexStr = "0" + hexStr;//前面补0
        }
        char[] chars = hexStr.toCharArray();
        int len = chars.length/2;
        byte[] bytes = new byte[len];
        for (int i = 0; i < len; i++) {
            int x = i*2;
            bytes[i] = (byte)Integer.parseInt(String.valueOf(new char[]{chars[x], chars[x+1]}), 16);
        }
        return bytes;
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            stringBuilder.append("0x");

            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
            if (i != src.length-1) {
                stringBuilder.append(",");

            }
        }
        return stringBuilder.toString();
    }
}
